﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DisplayBoardInfo.Server.Forms.Board
{
    /// <summary>
    /// Interaction logic for Board.xaml
    /// </summary>
    public partial class Board : UserControl
    {
        private Session.DataManager dataManager;
        private Entities.User user;

        public Entities.User Doctor
        {
            get { return user; }
        }

        public Board(Session.DataManager dataManager)
        {
            InitializeComponent();

            this.dataManager = dataManager;
            this.dataManager.StatusChanged += new EventHandler(dataManager_StatusChanged);
            this.dataManager.AppointmentsChanged += new EventHandler(dataManager_AppointmentsChanged);
            this.dataManager.NextPatient += new EventHandler(dataManager_NextPatient);

            this.user = dataManager.Doctor;
            drInfo.User = this.user;

            inRoomLabel.Hide();
        }

        void dataManager_NextPatient(object sender, EventArgs e)
        {
            this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new System.Windows.Threading.DispatcherOperationCallback(delegate
            {
                inRoomLabel.Patient = dataManager.InRoomPatient;
                return null;
            }), null);
        }

        void dataManager_AppointmentsChanged(object sender, EventArgs e)
        {
            this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new System.Windows.Threading.DispatcherOperationCallback(delegate
            {
                LoadAppointments();
                return null;
            }), null);
        }

        void dataManager_StatusChanged(object sender, EventArgs e)
        {
            this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new System.Windows.Threading.DispatcherOperationCallback(delegate
            {
                this.SessionStatus.Status = dataManager.CurrentStatus;
                if (dataManager.CurrentStatus == Session.SessionStatus.Przerwa) inRoomLabel.Hide();

                return null;
            }), null);
            
        }

        private void mainRowLabel_Loaded(object sender, RoutedEventArgs e)
        {
            Cursor = Cursors.Wait;

            LoadAppointments();

            Cursor = Cursors.Arrow;
        }

        private void LoadAppointments()
        {
            IEnumerable<Network.Messages.Content.Appointment> appointments = dataManager.Appointments.OrderBy(i => i.Number);
            if (appointments.Count() > 0)
            {
                List<Network.Messages.Content.Appointment> results = new List<Network.Messages.Content.Appointment>();
                int a = 1;
                int currentTime = 0;
                foreach (Network.Messages.Content.Appointment item in appointments)
                {
                    Network.Messages.Content.Appointment toAdd = item;
                    int tmp = toAdd.ExpectedTime;
                    toAdd.ExpectedTime = currentTime;
                    currentTime += tmp;

                    if (a > 1)
                    {
                        results.Add(toAdd);
                    }
                    a++;
                }

                patientsList.Appointments = results;


                Network.Messages.Content.Appointment first = appointments.First();
                mainRow.PatientNumber = (int)first.Number;
                mainRow.PatientName = first.Patient.Name + " " + first.Patient.LastName;
                mainRow.Time = (int)first.ExpectedTime;
            }
            else mainRow.Empty();
        }

        public override string ToString()
        {
            return dataManager.Doctor.ToString();
        }
    }
}
